from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from lib.lib import scanAllImages

class fileDock(QObject):
    newCanvas = pyqtSignal()
    def __init__(self, parent, name, directory=None):
        super(fileDock, self).__init__()
        self.mImgList = []
        self.filePath = None
        self.directory = directory
        self.parent = parent

        self.fileListWidget = QListWidget()

        self.new_dir = QPushButton('添加')
        self.next = QPushButton('下一个')
        self.prev = QPushButton('上一个')
        buttonLayout = QHBoxLayout()
        buttonLayout.addWidget(self.new_dir)
        buttonLayout.addWidget(self.next)
        buttonLayout.addWidget(self.prev)

        buttonContainer = QWidget()
        buttonContainer.setLayout(buttonLayout)

        filelistLayout = QVBoxLayout()
        filelistLayout.setContentsMargins(0, 0, 0, 0)
        filelistLayout.addWidget(self.fileListWidget)
        filelistLayout.addWidget(buttonContainer)

        fileListContainer = QWidget()
        fileListContainer.setLayout(filelistLayout)

        self.filedock = QDockWidget(name, self.parent)
        self.filedock.setObjectName(u'Files')
        self.filedock.setWidget(fileListContainer)
        self.filedock.setFeatures(QDockWidget.DockWidgetFloatable)

    def getdir(self):
        if self.directory == []:
            self.directory.append(QFileDialog.getExistingDirectory(self.parent,
                                                           "选取文件夹",
                                                           "/home/"))
        else:
            self.directory[0] = QFileDialog.getExistingDirectory(self.parent,
                                                              "选取文件夹",
                                                              self.directory[0])

        self.fileListWidget.clear()
        self.mImgList = scanAllImages(self.directory[0])
        self.filePath = None

        for imgPath in self.mImgList:
            item = QListWidgetItem(imgPath)
            self.fileListWidget.addItem(item)

        self.openNextImg()

    def clear(self):
        self.fileListWidget.clear()
        self.mImgList.clear()

    def addFile(self, fname):
        item = QListWidgetItem(fname)
        self.fileListWidget.addItem(item)
        self.mImgList.append(fname)

    def fileitemClicked(self, item=None):
        currIndex = self.mImgList.index(item.text())
        if currIndex < len(self.mImgList):
            filename = self.mImgList[currIndex]
            self.filePath = filename
            self.loadFile()

    def openPrevImg(self):

        if len(self.mImgList) <= 0:
            return
        if self.filePath is None:
            return

        currIndex = self.mImgList.index(self.filePath)
        if currIndex - 1 >= 0:
            filename = self.mImgList[currIndex - 1]
            self.filePath = filename

        self.loadFile()

    def openNextImg(self):
        if len(self.mImgList) <= 0:
            return

        filename = None
        if self.filePath is None:
            filename = self.mImgList[0]

        else:
            currIndex = self.mImgList.index(self.filePath)
            if currIndex + 1 < len(self.mImgList):
                filename = self.mImgList[currIndex + 1]

        if filename:
            self.filePath = filename
            self.loadFile()


    def loadFile(self):
        if self.filePath and self.fileListWidget.count() > 0:
            index = self.mImgList.index(self.filePath)
            fileWidgetItem = self.fileListWidget.item(index)
            fileWidgetItem.setSelected(True)

            self.newCanvas.emit()